<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// 生成路由
if (!function_exists('buildRoute')) {
    function buildRoute($routeData, $data)
    {
        foreach ($data as $value) {
            Route::name($value['name'])
                ->prefix($value['prefix'])
                ->middleware(['copyright-sign', \App\Miya\Middlewares\UserView::class])
                ->namespace('UI')
                ->group(function () use ($routeData, $value) {
                    foreach ($routeData as $name => $routeInfo) {
                        Route::get($routeInfo[0], sprintf($routeInfo[1], $value['method']))->name($name);
                    }
                });
        }
    }
}

//copyright页面
Route::get('_copyright', 'CopyrightController@index');
if (config('app.debug')) {
    //帮助信息.仅在网站制作阶段有效
    Route::get('_help/{name?}', 'HelpController@page')->name('_help');
}
//sitemap
Route::get('sitemap', 'UI\SitemapController@index')->name('sitemap');
//聊天页面
Route::get("_chat", 'UI\ChatController@index')->name('chat');

// 路由格式
$routeData = [
    'index' => ['/', 'IndexController@%s'],
    'search' => ['search', 'SearchController@%s'],
    'module' => ['{module:tag}.html', 'ModuleController@%s'],
    'classify' => ['{module:tag}/{classify:tag?}', 'ClassifyController@%s'],
    'info' => ['{module:tag}/{classify:tag}/{info:tag}.html', 'InfoController@%s'],
    'derivative' => ['{module:tag}/{classify:tag}/{info:tag}/{derivative}.html', 'InfoController@%s'],
];

$dataWithDomain = [
    ['name' => 'domain_area.', 'prefix' => '/', 'method' => 'area']
];
$data = [
    ['name' => 'area.', 'prefix' => 'area_{area:tag}', 'method' => 'area'],
    ['name' => 'm.', 'prefix' => 'm', 'method' => 'mobile'],
    ['name' => 'pc.', 'prefix' => '/', 'method' => 'pc'],
];

// 泛域名地区模式 不要解析www!!!!! 只能解析@和*
if (config('setting.open_domain_area_route') == 1) {
    $domain = sprintf('{area:tag}.%s', config('setting.area_domain'));
    Route::domain($domain)
        ->group(function () use ($routeData, $dataWithDomain) {
            buildRoute($routeData, $dataWithDomain);
        });
}

//正常域名
buildRoute($routeData, $data);
